import {computed, ref} from 'vue'
import {defineStore} from 'pinia'

export const useCounterStore = defineStore('counter', () => {
    const count = ref(0)
    const doubleCount = computed(() => count.value * 2)

    function increment() {
        count.value++
    }

    return {count, doubleCount, increment}
})

export const manageStore = defineStore("manageStore", () => {
    const status = ref([
        {
            code: "enable",
            name: "enable",
            value: "enable",
        },
        {
            code: "disable",
            name: "disable",
            value: "disable",
        }
    ])
    return {status}
});

export const rabbitmqStore = defineStore("rabbitmqStore", () => {
        const exchangeType = ref([
            {
                ukId: "1",
                name: "direct",
                value: "direct",
            },
            {
                ukId: "2",
                name: "fanout",
                value: "fanout",
            },
            {
                ukId: "3",
                name: "topic",
                value: "topic",
            },
            {
                ukId: "4",
                name: "headers",
                value: "headers",
            }
        ])
        const queueType = ref([
            {
                ukId: "1",
                name: "classic",
                value: "classic",
            },
            {
                ukId: "2",
                name: "quorum",
                value: "quorum",
            },
            {
                ukId: "3",
                name: "stream",
                value: "stream",
            }
        ])
        const durable = ref([
            {
                ukId: "0",
                name: "true",
                value: "true",
            },
            {
                ukId: "1",
                name: "false",
                value: "false",
            }
        ])
        const auto_delete = ref([
            {
                ukId: "0",
                name: "true",
                value: "true",
            },
            {
                ukId: "1",
                name: "false",
                value: "false",
            }
        ])
        const internal = ref([
            {
                ukId: "0",
                name: "true",
                value: "true",
            },
            {
                ukId: "1",
                name: "false",
                value: "false",
            }
        ])
        const payloadEncoding = ref([
            {
                ukId: "0",
                name: "string",
                value: "string",
            },
            {
                ukId: "1",
                name: "base64",
                value: "base64",
            }
        ])
        const msgRates = ref([
            {
                ukId: "0",
                name: "1 分钟",
                value: "60_5",
            },
            {
                ukId: "1",
                name: "5 分钟",
                value: "300_10",
            },
            {
                ukId: "4",
                name: "10 分钟",
                value: "600_30",
            },
            {
                ukId: "5",
                name: "30 分钟",
                value: "1800_60",
            },
            {
                ukId: "6",
                name: "1 小时",
                value: "3600_120",
            },
            {
                ukId: "7",
                name: "2 小时",
                value: "7200_360",
            },
            {
                ukId: "8",
                name: "5 小时",
                value: "18000_720",
            },
        ])
        const role = ref([
            {
                ukId: "0",
                name: "administrator",
                value: "administrator",
            },
            {
                ukId: "1",
                name: "monitoring",
                value: "monitoring",
            },
            {
                ukId: "2",
                name: "policymaker",
                value: "policymaker",
            },
            {
                ukId: "3",
                name: "management",
                value: "management",
            },
            {
                ukId: "4",
                name: "impersonator",
                value: "impersonator",
            },
        ])
        const applyToType = ref([
            {
                ukId: "0",
                name: "exchanges and queues",
                value: "all",
            },
            {
                ukId: "1",
                name: "exchanges",
                value: "exchanges",
            },
            {
                ukId: "2",
                name: "queues",
                value: "queues",
            },
            {
                ukId: "3",
                name: "classic_queues",
                value: "classic_queues",
            },
            {
                ukId: "4",
                name: "quorum_queues",
                value: "quorum_queues",
            },
            {
                ukId: "5",
                name: "streams",
                value: "streams",
            },
        ])
        const refreshTime = ref([
            {
                ukId: "0",
                name: "3 秒",
                value: 3000,
            },
            {
                ukId: "1",
                name: "5 秒",
                value: 5000,
            },
            {
                ukId: "2",
                name: "10 秒",
                value: 10000,
            },
            {
                ukId: "3",
                name: "15 秒",
                value: 15000,
            },
            {
                ukId: "4",
                name: "30 秒",
                value: 30000,
            }
        ])
        const refreshInterval = ref(3000)

        function setRefreshInterval(interval) {
            refreshInterval.value = interval
        }

        const timer = ref()

        function setTimer(func) {
            timer.value = setInterval(() => {
                func()
            }, refreshInterval.value)
        }

        function deleteTimer() {
            timer.value = null
        }

        const policyAllType = ref([
            {
                name: "max-length",
                value: "max-length",
                type: "number"
            },
            {
                name: "max-length-bytes",
                value: "max-length-bytes",
                type: "number"
            },
            {
                name: "message-ttl",
                value: "message-ttl",
                type: "number"
            },
            {
                name: "dead-letter-exchange",
                value: "dead-letter-exchange",
                type: "text"
            },
            {
                name: "dead-letter-routing-key",
                value: "dead-letter-routing-key",
                type: "text"
            }
        ])

        const parameterList = ref([
            {
                label: "mem used rate",
                value: "MemUsedRate",
                type: "text"
            },
            {
                label: "disk free rate",
                value: "DiskFreeRate",
                type: "text"
            },
        ])

        const memUsedRate = ref("0.7")

        function setMemUsedRate(rate){
            memUsedRate.value = rate
        }

        const diskFreeRate = ref("0.3")

        function setDiskFreeRate(rate){
            diskFreeRate.value = rate
        }

        return {
            exchangeType,
            queueType,
            durable,
            auto_delete,
            internal,
            payloadEncoding,
            msgRates,
            role,
            applyToType,
            refreshTime,
            refreshInterval,
            setRefreshInterval,
            timer,
            setTimer,
            deleteTimer,
            policyAllType,
            parameterList,
            memUsedRate,
            setMemUsedRate,
            diskFreeRate,
            setDiskFreeRate
        }
    },
    {
        persist: [
            {
                pick: ['refreshInterval'],
                storage: sessionStorage,
            }
        ],
    }
);

export const rabbitmqAuth = defineStore("rabbitmqAuth", () => {
        const basic = ref("")

        function setBasic(username, password) {
            let s = username.value + ":" + password.value;
            let base64 = btoa(s);
            basic.value = "Basic " + base64
        }
        return {basic, setBasic}
    },
    {
        persist: [
            {
                pick: ['basic'],
                storage: sessionStorage,
            }
        ],
    }
);
